package com.ts.led;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LedActivity extends Activity {
    private Button mLedBtn = null;
    private Intent mIntent = null;
    private boolean mIsOn = false; // 定义开关状态，状态为false，关闭状态，状态为true，打开状态
    private int mBackCount = 0;// 判断按几次back
    private SharedPreferences mSharedPref=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.led_layout);
        mSharedPref = getSharedPreferences(Ledserver.LED_STATE, Context.MODE_PRIVATE);

        mLedBtn = (Button) findViewById(R.id.btn_light);
        mIntent = new Intent(LedActivity.this, Ledserver.class);
        mLedBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIsOn = mSharedPref.getBoolean(Ledserver.LED_STATE, false);
                Log.e("xiajy", "mIsOn="+mIsOn);
                if (!mIsOn) {
                    mLedBtn.setBackgroundResource(R.drawable.shou_on);
                    LedActivity.this.startService(mIntent);
                    Editor editor = mSharedPref.edit();//获取编辑器
                    editor.putBoolean(Ledserver.LED_STATE, true);
                    editor.commit();//提交修改
                    sendBroadcast(new Intent("com.ts.led.UPDATE_LED_UI_ON"));
                    //mIsOn=true;
                } else {
                    mLedBtn.setBackgroundResource(R.drawable.shou_off);
                    LedActivity.this.stopService(mIntent);
                    Editor editor = mSharedPref.edit();//获取编辑器
                    editor.putBoolean(Ledserver.LED_STATE, false);
                    editor.commit();//提交修改
                    sendBroadcast(new Intent("com.ts.led.UPDATE_LED_UI_OFF"));
                    //mIsOn=false;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mIsOn = mSharedPref.getBoolean(Ledserver.LED_STATE, false);
        if (mIsOn) {
            mLedBtn.setBackgroundResource(R.drawable.shou_on);
        } else {
            mLedBtn.setBackgroundResource(R.drawable.shou_off);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            mBackCount++;
            switch (mBackCount) {
                case 1:
                    Toast.makeText(LedActivity.this, getString(R.string.again_exit), Toast.LENGTH_SHORT).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mBackCount = 0;
                        }
                    }, 2000);
                    break;
                case 2:
                    mBackCount = 0;
                    closeApp();
                    break;
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeApp();
    }

    public void closeApp() { // 关闭程序
        Editor editor = mSharedPref.edit();//获取编辑器
        editor.putBoolean(Ledserver.LED_STATE, false);
        editor.commit();//提交修改
        sendBroadcast(new Intent("com.ts.led.UPDATE_LED_UI_OFF"));
        Log.e("xiajy", "sendBroadcast=com.ts.led.UPDATE_LED_UI_OFF");
        mIsOn = mSharedPref.getBoolean(Ledserver.LED_STATE, false);
        if (mIsOn) {// 开关打开时
            this.stopService(mIntent);
            //mIsOn=false;
        }
        LedActivity.this.finish();
        android.os.Process.killProcess(android.os.Process.myPid());// 关闭进程
    }
}
